//
//  ScrollPhotoView+PlayFunction.m
//  NaRiLSVMT
//
//  Created by zhangmeng on 14-11-2.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "ScrollPhotoView+PlayFunction.h"
#import "ScrollPhotoView+AddopenView.h"
#import "ScrollPhotoView+Request.h"
#import "ScrollPhotoView.h"
#import "ScrollPhotoView+list.h"
@implementation ScrollPhotoView (PlayFunction)
-(void)onceplayClick:(id)sender
{
    //点击屏幕 播放 缩略图
    if([Common isEmptyString:self.secondNumTextf.text]){
        Alert(@"请输入间隔时间");
        return;
    }
    if(![Common isAllNum:self.secondNumTextf.text]){
        Alert(@"非法的间隔时间");
        return;
    }
    if([self.secondNumTextf.text intValue] < 5){
        Alert(@"间隔时间不能小于5s");
        return;
    }
    if([self.secondNumTextf.text intValue] > 99){
        Alert(@"间隔时间不能大于99s");
        return;
    }
    if(![(NSArray *)(self.nowPlaydata[StringFromInt(self.nowScreen)]) count] || [(NSArray *)(self.nowPlaydata[StringFromInt(self.nowScreen)]) count] == 1){
        return;
    }
    
    //停止循环播放
    if([self.nowPlaydata[@"noArrowplay"] intValue]){
        [self.arrowplay setBackgroundImage:[UIImage imageNamed:@"scan_arrowplay.png"] forState:UIControlStateNormal];
        self.nowPlaydata[@"noArrowplay"] = @"0";
        self.nowPlaydata[@"startqueue"] = @"0";
    }
    if(![self.nowPlaydata[@"noOnceplay"] intValue]){
        [self.onceplay setBackgroundImage:[UIImage imageNamed:@"scan_stopplay.png"] forState:UIControlStateNormal];
        self.nowPlaydata[@"noOnceplay"] = @"1";
        self.nowPlaydata[@"startqueue"] = @"1";
        ((MJCollectionViewController *)self.nowPlaydata[@"collec"]).flagplay = YES;
        if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
            [self.delegate playTalkinfo];
        }
        self.textFiledView.hidden = NO;
    }
    else{
        [self.onceplay setBackgroundImage:[UIImage imageNamed:@"scan_onceplay.png"] forState:UIControlStateNormal];
        self.nowPlaydata[@"noOnceplay"] = @"0";
        self.nowPlaydata[@"startqueue"] = @"0";
        ((MJCollectionViewController *)self.nowPlaydata[@"collec"]).flagplay = NO;
        self.forScreen = YES;
         self.textFiledView.hidden = [self isShowTextView];
        return;
    }
    
    MJCollectionViewController *collectionview  = self.nowPlaydata[@"collec"];
    if(collectionview.figureNum >=0){
        collectionview.clickNum = collectionview.figureNum;
    }
    if(collectionview.figureNum > -1){
        NSDictionary *dic = self.nowPlaydata[StringFromInt(self.nowScreen)][collectionview.figureNum];
        NSString *host = dic[@"hostId"];
        self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:[host componentsSeparatedByString:@","]];
    }
    else{
        NSDictionary *dic = self.nowPlaydata[StringFromInt(self.nowScreen)][0];
        NSString *host = dic[@"hostId"];
        self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:[host componentsSeparatedByString:@","]];
        collectionview.clickNum = collectionview.figureNum = 0;
    }
    
    __block  NSMutableArray *hostarr = self.nowPlaydata[@"proharr"];
    NSArray *dataarr = self.nowPlaydata[StringFromInt(self.nowScreen)];
    NSString *palayScreen = StringFromInt(self.nowScreen);
    __block NSMutableDictionary *tmpdic = self.nowPlaydata;
    NSInteger time_delay = [self.secondNumTextf.text intValue];
    __block int flag = 0;
    __block NSInteger i = 0;
    dispatch_queue_t queue = self.nowPlaydata[@"queue"];
    dispatch_async(queue,^{
        MJCollectionViewController *collectionview  = self.nowPlaydata[@"collec"];
        for(;i < dataarr.count;i++){
            [NSThread sleepForTimeInterval:time_delay];
            if([palayScreen isEqualToString:StringFromInt(self.nowScreen)]){
                tmpdic = self.nowPlaydata;
            }
            if([tmpdic[@"startqueue"] intValue] && [tmpdic[@"noOnceplay"] intValue]){
                dispatch_async(dispatch_get_main_queue(), ^{
                    if(collectionview.clickNum < (dataarr.count-1)){
                        if(!flag && (collectionview.clickNum == 0)){
                            collectionview.clickNum = 0;
                            flag = 1;
                        }
                         if([dataarr[collectionview.clickNum+1][@"width"] intValue] >0 && [dataarr[collectionview.clickNum+1][@"height"] intValue] >0){
                            
                            NSString *scerrnId = StringFromId(self.screenArr[[tmpdic[@"numscreen"]intValue]][@"id"]);
                            if([scerrnId isEqualToString:self.screenId]){
                            [self eventNumData:collectionview.clickNum+1 AllNUm:collectionview.allPage];
                            [self.showImage setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(dataarr[collectionview.clickNum+1][@"templateId"]) Block:^(UIImage *image){
                                [self.showImage setImage:image];
                            }]];
                            [[TimerDownImage sharedInstance] downCallBackData:StringFromId(dataarr[collectionview.clickNum+1][@"templateId"])  CallBack:^(UIImage *image){
                                [self.showImage setImage:image];
                            }];
                            }
                        }
                        else{
                            [[TimerDownImage sharedInstance] stopTimerDownImage];
                            self.showImage.image = nil;
                            //Alert(@"height或width为空");
                        }
                        //发送tcp切屏命令
                        [self eventScreenSendTcpTrue:dataarr[collectionview.clickNum+1][@"hostId"]];
                        if(![hostarr count] || hostarr == nil){
                            hostarr = [NSMutableArray arrayWithArray:[dataarr[collectionview.clickNum+1][@"hostId"] componentsSeparatedByString:@","]];
                            self.proHostArr = [NSMutableArray arrayWithArray:hostarr];
                        }
                        if(![self proHostIdIsEmpry:dataarr[collectionview.clickNum+1][@"hostId"]]){
                            [self eventScreenSendTcpFalse:hostarr];
                            hostarr = [NSMutableArray arrayWithArray:[dataarr[collectionview.clickNum+1][@"hostId"] componentsSeparatedByString:@","]];
                            self.proHostArr = [NSMutableArray arrayWithArray:hostarr];
                        }
                    }
                    if(collectionview.clickNum > 1){
                        if(self.openroclose){
                            if((collectionview.clickNum+1)%OPENPAGE == 0){
                                [self nextPlaypage:collectionview];
                            }
                        }
                        else{
                            if((collectionview.clickNum+1)%CLOSEPAGE == 0){
                                [self nextPlaypage:collectionview];
                            }
                        }
                    }
                    //播放到最后
                    if(i == (dataarr.count-1)){
                        //   [collectionview.collectionView reloadData];
//                        collectionview.clickNum +=1;
//                        collectionview.figureNum = collectionview.clickNum;
                        [collectionview.collectionView reloadData];
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [self.onceplay setBackgroundImage:[UIImage imageNamed:@"scan_onceplay.png"] forState:UIControlStateNormal];
                            self.textFiledView.hidden = [self isShowTextView];

                        });
                        tmpdic[@"noOnceplay"] = @"0";
                        tmpdic[@"startqueue"] = @"0";
                        collectionview.flagplay = NO;
                        self.forScreen = YES;
                        return;
                    }
                    else{
                        [collectionview.collectionView reloadData];
                        collectionview.clickNum +=1;
                    }
                    if(collectionview.clickNum == dataarr.count){
                        collectionview.clickNum = 0;
                        collectionview.pagenum = 0;
                        collectionview.pageBtnnum = 1;
                        
                        flag = 0;
                        if([dataarr[0][@"width"] intValue] >0 && [dataarr[0][@"height"] intValue] >0){
                            NSString *scerrnId = StringFromId(self.screenArr[[tmpdic[@"numscreen"]intValue]][@"id"]);
                            if([scerrnId isEqualToString:self.screenId]){
                                [self eventNumData:0 AllNUm:collectionview.allPage];
                                [self.showImage setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(dataarr[0][@"templateId"]) Block:^(UIImage *image){
                                    [self.showImage setImage:image];
                                }]];
                                
                                [[TimerDownImage sharedInstance] downCallBackData:StringFromId(dataarr[0][@"templateId"])  CallBack:^(UIImage *image){
                                    [self.showImage setImage:image];
                                }];
                            }
                        }
                        else{
                            
                            [[TimerDownImage sharedInstance] stopTimerDownImage];
                            self.showImage.image = nil;
                        }
                        
                        [self eventScreenSendTcpTrue:dataarr[collectionview.clickNum][@"hostId"]];
                        if(![hostarr count] || hostarr == nil){
                            hostarr = [NSMutableArray arrayWithArray:[dataarr[collectionview.clickNum] componentsSeparatedByString:@","]];
                            self.proHostArr = [NSMutableArray arrayWithArray:hostarr];
                            self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:hostarr];
                            
                        }
                        else{ //if([self proHostIdIsEmpry:dataarr[collectionview.clickNum+1][@"hostId"]]){
                            //if(![hostarr count] || hostarr == nil){
                            [self eventScreenSendTcpFalse:hostarr];
                            
                            hostarr = [NSMutableArray arrayWithArray:[dataarr[collectionview.clickNum][@"hostId"] componentsSeparatedByString:@","]];
                            self.proHostArr = [NSMutableArray arrayWithArray:hostarr];
                            self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:hostarr];
                        }
                    }
                });
            }
            else{
                i = dataarr.count+2;
            }
            
        }
    });
    
    
}

-(void)arrowplayClick:(id)sender
{
    //点击屏幕 播放 缩略图
    if([Common isEmptyString:self.secondNumTextf.text]){
        Alert(@"请输入间隔时间");
        return;
    }
    if(![Common isAllNum:self.secondNumTextf.text]){
        Alert(@"非法的间隔时间");
        return;
    }
    if([self.secondNumTextf.text intValue] < 5){
        Alert(@"间隔时间不能小于5s");
        return;
    }
    if([self.secondNumTextf.text intValue] > 99){
        Alert(@"间隔时间不能大于99s");
        return;
    }
    if(![(NSArray *)(self.nowPlaydata[StringFromInt(self.nowScreen)]) count] || [(NSArray *)(self.nowPlaydata[StringFromInt(self.nowScreen)]) count] == 1){
        return;
    }
    //停止单次播放
    if([self.nowPlaydata[@"noOnceplay"] intValue]){
        [self.onceplay setBackgroundImage:[UIImage imageNamed:@"scan_onceplay.png"] forState:UIControlStateNormal];
        self.nowPlaydata[@"noOnceplay"] = @"0";
        self.nowPlaydata[@"startqueue"] = @"0";
    }
    //开始循环播放
    if(![self.nowPlaydata[@"noArrowplay"] intValue]){
        [self.arrowplay setBackgroundImage:[UIImage imageNamed:@"scan_stopplay.png"] forState:UIControlStateNormal];
        ((MJCollectionViewController *)self.nowPlaydata[@"collec"]).flagplay = YES;
        self.nowPlaydata[@"noArrowplay"] = @"1";
        self.nowPlaydata[@"startqueue"] = @"1";
        if([self.delegate respondsToSelector:@selector(panTalkinfo)]){
            [self.delegate playTalkinfo];
        }
         self.textFiledView.hidden = NO;
    }
    //关闭循环播放
    else{
        [self.arrowplay setBackgroundImage:[UIImage imageNamed:@"scan_arrowplay.png"] forState:UIControlStateNormal];
        self.nowPlaydata[@"noArrowplay"] = @"0";
        self.nowPlaydata[@"startqueue"] = @"0";
        ((MJCollectionViewController *)self.nowPlaydata[@"collec"]).flagplay = NO;
        self.forScreen = YES;
        self.textFiledView.hidden = [self isShowTextView];
        return;
    }
    MJCollectionViewController *collectionview  = self.nowPlaydata[@"collec"];
    if(collectionview.figureNum >=0){
        collectionview.clickNum = collectionview.figureNum;
    }
    if(collectionview.figureNum > -1){
        NSDictionary *dic = self.nowPlaydata[StringFromInt(self.nowScreen)][collectionview.figureNum];
        NSString *host = dic[@"hostId"];
        self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:[host componentsSeparatedByString:@","]];
    }
    else{
        NSDictionary *dic = self.nowPlaydata[StringFromInt(self.nowScreen)][0];
        NSString *host = dic[@"hostId"];
        self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:[host componentsSeparatedByString:@","]];
        collectionview.clickNum = collectionview.figureNum = 0;
    }
    dispatch_queue_t queue = self.nowPlaydata[@"queue"];
    //启动线程
    dispatch_async(queue,^{
        MJCollectionViewController *collectionview  = self.nowPlaydata[@"collec"];
        NSArray *dataarr = self.nowPlaydata[StringFromInt(self.nowScreen)];
        __block  NSMutableArray *hostarr = self.nowPlaydata[@"proharr"];
        NSInteger time_delay = [self.secondNumTextf.text intValue];
        NSString *palayScreen = StringFromInt(self.nowScreen);
        NSDictionary *tmpdic = self.nowPlaydata;
        __block int flag = 0;
        while(1){
            [NSThread sleepForTimeInterval:time_delay];
            if([palayScreen isEqualToString:StringFromInt(self.nowScreen)]){
                tmpdic = self.nowPlaydata;
            }
            if([tmpdic[@"startqueue"] intValue] && [tmpdic[@"noArrowplay"] intValue]){
                dispatch_async(dispatch_get_main_queue(), ^{
                    if(collectionview.clickNum < (dataarr.count-1)){
                        if(!flag && (collectionview.clickNum == 0)){
                            // if(collectionview.clickNum == 0){
                            collectionview.clickNum = 0;
                            flag = 1;
                            //}
                        }
                        if([dataarr[collectionview.clickNum+1][@"width"] intValue] >0 && [dataarr[collectionview.clickNum+1][@"height"] intValue] >0){
                            NSString *scerrnId = StringFromId(self.screenArr[[tmpdic[@"numscreen"]intValue]][@"id"]);
                            if([scerrnId isEqualToString:self.screenId]){
                            [self eventNumData:collectionview.clickNum+1 AllNUm:collectionview.allPage];
                            [self.showImage setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(dataarr[collectionview.clickNum+1][@"templateId"]) Block:^(UIImage *image){
                                [self.showImage setImage:image];
                            }]];
                            
                            [[TimerDownImage sharedInstance] downCallBackData:StringFromId(dataarr[collectionview.clickNum+1][@"templateId"])  CallBack:^(UIImage *image){
                                [self.showImage setImage:image];
                            }];
                            }
                        }
                        else{
                            
                            [[TimerDownImage sharedInstance] stopTimerDownImage];
                            self.showImage.image = nil;
                        }
                        //发送tcp切屏命令
                        // NSArray *dataarr = self.nowPlaydata[palayScreen];
                       
                        [self eventScreenSendTcpTrue:dataarr[collectionview.clickNum+1][@"hostId"]];
                        if(![hostarr count] || hostarr == nil){
                            hostarr = [NSMutableArray arrayWithArray:[dataarr[collectionview.clickNum+1] componentsSeparatedByString:@","]];
                            self.proHostArr = [NSMutableArray arrayWithArray:hostarr];
                            self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:hostarr];

                        }
                        else{ //if([self proHostIdIsEmpry:dataarr[collectionview.clickNum+1][@"hostId"]]){
                            //if(![hostarr count] || hostarr == nil){
                            [self eventScreenSendTcpFalse:hostarr];
                            
                            hostarr = [NSMutableArray arrayWithArray:[dataarr[collectionview.clickNum+1][@"hostId"] componentsSeparatedByString:@","]];
                            self.proHostArr = [NSMutableArray arrayWithArray:hostarr];
                            self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:hostarr];
                        }
                        
                    }
                if(collectionview.clickNum >1){
                        if(self.openroclose){
                            if((collectionview.clickNum+1)%OPENPAGE == 0){
                                [self nextPlaypage:collectionview];
                            }
                        }
                        else{
                            if((collectionview.clickNum+1)%CLOSEPAGE == 0){
                                [self nextPlaypage:collectionview];
                            }
                        }
                    }
                    [collectionview.collectionView reloadData];
                    collectionview.clickNum +=1;
                    //播放到最后
                    if(collectionview.clickNum == dataarr.count){
                        collectionview.clickNum = 0;
                        collectionview.pagenum = 0;
                        collectionview.pageBtnnum = 1;
                        flag = 0;
                        if([dataarr[0][@"width"] intValue] >0 && [dataarr[0][@"height"] intValue] >0){
                            NSString *scerrnId = StringFromId(self.screenArr[[tmpdic[@"numscreen"]intValue]][@"id"]);
                            if([scerrnId isEqualToString:self.screenId]){
                                [self eventNumData:0 AllNUm:collectionview.allPage];
                                [self.showImage setImage:[[DownloadImage sharedInstance] getUrlImage:StringFromId(dataarr[0][@"templateId"]) Block:^(UIImage *image){
                                    [self.showImage setImage:image];
                                }]];
                                
                                [[TimerDownImage sharedInstance] downCallBackData:StringFromId(dataarr[0][@"templateId"])  CallBack:^(UIImage *image){
                                    [self.showImage setImage:image];
                                }];
                            }
                        }
                        else{
                            
                            [[TimerDownImage sharedInstance] stopTimerDownImage];
                            self.showImage.image = nil;
                        }

                        [self eventScreenSendTcpTrue:dataarr[collectionview.clickNum][@"hostId"]];
                        if(![hostarr count] || hostarr == nil){
                            hostarr = [NSMutableArray arrayWithArray:[dataarr[collectionview.clickNum] componentsSeparatedByString:@","]];
                            self.proHostArr = [NSMutableArray arrayWithArray:hostarr];
                            self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:hostarr];
                            
                        }
                        else{ //if([self proHostIdIsEmpry:dataarr[collectionview.clickNum+1][@"hostId"]]){
                            //if(![hostarr count] || hostarr == nil){
                            [self eventScreenSendTcpFalse:hostarr];
                            
                            hostarr = [NSMutableArray arrayWithArray:[dataarr[collectionview.clickNum][@"hostId"] componentsSeparatedByString:@","]];
                            self.proHostArr = [NSMutableArray arrayWithArray:hostarr];
                            self.nowPlaydata[@"proharr"] = [NSMutableArray arrayWithArray:hostarr];
                        }
                    }
                });
            }
            else{
                break;
            }
        }
    });
}
- (BOOL)isShowTextView
{
    BOOL isFlag = YES;
    for(NSDictionary *dic in self.thumbScreen){
        if([dic[@"noOnceplay"] intValue] || [dic[@"noArrowplay"] intValue]){
            isFlag = NO;
            break;
        }
    }
    return isFlag;
}
@end
